<?php

	require_once(dirname(__FILE__)."/../core/safeParam.php");
	
	class main{
		// ==================================================================
  		// Add News.
  		function listSubscribe($vars){
  			global $database;
    		$id         = $vars['id'];
    		$name       = $vars['name'];
    		$subscribed = '1';
    		$email      = $vars['email'];
    		$return     = $vars['return'];
    		$query      = $database->insert("users", "name, subscribed, email", "'$name', '$subscribed', '$email'");
    		header('location: ' . $return . '&signup-success=1');
  		}
  		
  		// ==================================================================
  		// Modify News.
  		function listUnubscribe($vars){
    		global $database;
    		$id         = $vars['id'];
    		$date       = $vars['date'];
    		$name       = $vars['name'];
    		$subscribed = '0';
    		$email      = $vars['email'];
    		$return     = base64_decode($vars['return']);
    		$query      = $database->update("users", "subscribed = '$subscribed'", "id = '$id'");
    		
    		if (!$query) {
    			echo 'error';
      		exit;
    		} //!$query
    		
    		header('location: ' . $return . '&success=1');
  		}
  		
  		// ==================================================================
  		// Modify News.
  		function listDelete($vars){
    		global $database;
    		$id     = $vars['id'];
    		$return = base64_decode($vars['return']);
    		$query  = $database->delete("users", "id = '$id'");
    		header('location: ' . $return . '&success=1');
  		}
  		
  		function multipleDelete($vars){
    		global $database;
    		$delete    = $vars['delete'];
    		$return    = $vars['return'];
    		$return    = base64_decode($return);
    		$deletesep = implode(",", $delete);
    		$query     = $database->delete("users", "id IN (" . $deletesep . ")");
    		header('location: ' . $return . '&success=1');
  		}
  		
  		function sendNewsletter($vars){
    		global $database;
    		$message = $vars['message'];
    		$subject = $vars['subject'];
    		$return  = base64_decode($vars['return']);
    		
    		// find the email we are sending from
    		$query   = $database->query("SELECT * FROM config WHERE id = 1 ");
    		
    		while ($list = mysql_fetch_array($query)) {
    			$id          = $list['id'];
      		$siteroot    = $list['siteroot'];
      		$sitename    = $list['sitename'];
      		$username    = $list['username'];
	     		$password    = $list['password'];
      		$email       = $list['email'];
      		$description = $list['description'];
      		$keywords    = $list['keywords'];
      		$layout      = $list['layout'];
      		$address     = $list['address'];
      		$address2    = $list['address2'];
      		$city        = $list['city'];
      		$state       = $list['state'];
      		$phone       = $list['phone'];
      		$fax         = $list['fax'];
    		} //$list = mysql_fetch_array($query)
    		
    		// query for users
    		$query2 = $database->query("SELECT * FROM users");
    		
    		while ($list = mysql_fetch_array($query2)) {
      		$listid         = $list['id'];
      		$listname       = $list['name'];
      		$listsubscribed = $list['subscribed'];
      		$listemail      = $list['email'];
      		$emails .= '' . $listemail . ' ';
    		} //$list = mysql_fetch_array($query2)
    		
    		$emails  = explode(" ", $emails);
    		$howmany = mysql_num_rows($query2);
    		
    		//send mail
    		$m       = new Mail; // create the mail
    		$m->From(ADMIN_EMAIL);
    		$m->To($email);
    		$m->Bcc($emails);
    		$m->Subject("Newsletter from " . SITE_NAME . " : $subject");
    		$m->Body($message); // set the body	
    		$m->Priority(4); // set the priority to Low	
    		$m->Send(); // send the mail
    		header('location: ' . $return . '&success=1&addmessage=' . $howmany . '-emails-sent');
	}
	
	function addFeaturedListing($vars){
		global $database;
    	$bid   = $vars['bid'];
    	$query = $database->query("SELECT * FROM listings_name where bid = '$bid'");
    	
    	while ($list = mysql_fetch_array($query)) {
    		$bid  = $list['bid'];
      	$name = $list['name'];
    	} //$list = mysql_fetch_array($query)
    	
    	$query = $database->query("SELECT * FROM listings_type where bid = '$bid'");
    	
    	while ($list = mysql_fetch_array($query)) {
    		$type = $list['type'];
    	} //$list = mysql_fetch_array($query)
    	
    	$query = $database->insert("listings_featured", "bid, name, type", "'$bid', '$name', '$type'");
    	header('location: ../administrator/admin.php?page=bbb&success=1');
  	}
  
  	function addImageFeaturedListing($vars, $files){
  		global $database;
    	$isiet  = $vars['name'];
    	$id     = $vars['id'];
    	$level2 = $vars['level'];
    	
    	if ($level2 == '') {
    		$level = '0';
    	} else {
    		$level = '1';
    	}
    	
    	$file['tmp']   = $files['name']['tmp_name'];
    	$file['type']  = $files['name']['type'];
    	$file['size']  = $files['name']['size'];
    	$allowed_types = array( 'image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/bmp');
    	
    	if ($file['tmp'] != "" && is_uploaded_file($file['tmp'])) {
    		if (!in_array($file['type'], $allowed_types)) {
    			echo 'Error!';
        		exit();
      	} //!in_array($file['type'], $allowed_types)
      	
      	if ($file['size'] == 0) {
      		echo 'Error!';
        		exit();
      	} //$file['size'] == 0
      	
      	switch ($file['type']) {
      		case 'image/gif';
          		$ext = "gif";
          		break;
        		
        		case 'image/jpeg';
          		$ext = "jpg";
          		break;
        		
        		case 'image/pjpeg';
          		$ext = "jpg";
          		break;
        		
        		case 'image/png';
          		$ext = "png";
          		break;
        		
        		case 'image/bmp';
          		$ext = "bmp";
          		break;
      	} //$file['type']
      	
      	srand(time());
      	$filename            = (rand() % 1000000000);
      	$photo               = $filename . "_thumb." . $ext;
      	$uploaded_file       = '../images/members/' . $filename . "." . $ext;
      	$uploaded_file_thumb = '../images/members/' . $filename . "_thumb." . $ext;
      	$rimg                = new RESIZEIMAGE($file['tmp']);
      	$rimg->resize_limitwh("750", "350", $uploaded_file);
      	$rimg->resize_limitwh("100", "110", $uploaded_file_thumb);
      	$rimg->close();
      	$query = $database->update("listings_featured", "photo = '$photo',level = '$level'", "id = '$id'");
    	} else {
    		$query = $database->update("listings_featured", "level = '$level'", "id = '$id'");
    	}
    	
    	header('location: ../administrator/admin.php?page=bbb&success=1');
  }
  
  function deleteFeaturedListing($vars){
  		global $database;
    	$bid   = $vars['bid'];
    	$query = $database->delete("listings_featured", "bid = '$bid'");
    	header('location: ../administrator/admin.php?page=bbb&success=1');
  }
  // End of Class
  // ==================================================================
}
?>
